package com.cn.utils;

import com.alibaba.fastjson.JSON;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.*;

/**
 * Created by chenyuncong on 2018/8/3.
 */
public class TimeUtils {
    /**
     * 转成当天开始时间的时间戳 如 2017-05-24 11:57:45 --> 2017-05-24 00:00:00 --> 1495555200000
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static long strDayToTime(String date)  {

        try {
            return strToTime(date,"yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
    public static long strToTime(String date)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d.getTime();
    }

    public static long strToTime(String date, String pattern) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date d = simpleDateFormat.parse(date);
        return d.getTime();
    }

    public static Date strDayToDate(String date){
        return strToDate(date,"yyyy-MM-dd");
    }

    public static Date strToDate(String date){
        return strToDate(date,"yyyy-MM-dd HH:mm:ss");
    }
    public static Date strToDate(String date,String pattern)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date d = null;
        try {
            d = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static String timeToStr(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(time);
        return simpleDateFormat.format(date);
    }

    public static String timeToStr(long time, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date(time);
        return simpleDateFormat.format(date);
    }

    public static String strToMongDate(String date){
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//        simpleDateFormat2.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        String str=null;
        try {
            str = simpleDateFormat2.format(simpleDateFormat1.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return str;
    }


/*    public static Date mongoDateToDate(Date mDate){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = simpleDateFormat.parse(mDate.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }*/

    public static int dayForWeek(String pTime) throws Throwable {
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(pTime));
        int dayForWeek = 0;
        if(c.get(Calendar.DAY_OF_WEEK) == 1){
            dayForWeek = 7;
        }else{
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    /**
     * 获取当前时间的秒数
     * @return
     */
    public static int nowSecond(){
        LocalDateTime now =  LocalDateTime.now();
        //获取小时
        int hour = now.getHour();
        //获取分钟
        int minute = now.getMinute();
        //获取秒
        int second = now.getSecond();
        int sec = hour*60*60+minute*60+second;
        return sec;
    }

    public static void main(String[] args) throws Throwable {

        System.out.println(dayForWeek("2018-09-09"));

//

        System.out.println(timeToStr(1536249600000L));

        List<TestTime> list = new ArrayList<>();

        list.add(new TestTime("a2",strToDate("2018-08-13 10:41:02")));
        list.add(new TestTime("a3",strToDate("2018-08-13 10:42:02")));
        list.add(new TestTime("a1",strToDate("2018-08-13 10:32:02")));

        list.sort((t1,t2)-> t1.getDate().compareTo(t2.getDate()));
        list.forEach(x->{
            System.out.println(JSON.toJSONString(x));
        });

        List<TestTime> list2 = new ArrayList<>();

        list2.add(list.get(1));
        list2.add(list.get(2));

        list2.forEach(x->{
            x.setName(x.getName()+"-"+x.getName());
        });

        list.forEach(x->{
            System.out.println(JSON.toJSONString(x));
        });

        System.out.println(strToTime("2018-08-15 00:00:00"));
        System.out.println(strToTime("2018-08-15 23:59:59"));


        LocalDateTime now =  LocalDateTime.now();// LocalDateTime.of(2018, 6, 10, 10, 10, 10);
        //获取年
        int year = now.getYear();
        //获取月
        Month month = now.getMonth();
        //获取月的value值
        int monthValue = now.getMonthValue();
        //获取在本月的第几天（日）
        int dayOfMonth = now.getDayOfMonth();
        //获取在本年的第几天
        int dayOfYear = now.getDayOfYear();
        //获取在本周的第几天(星期几)
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        System.out.println(dayOfWeek);
        //获取小时
        int hour = now.getHour();
        System.out.println(hour);
        //获取分钟
        int minute = now.getMinute();
        System.out.println(minute);
        //获取秒
        int second = now.getSecond();
        System.out.println(second);
        //获取纳秒
        int nano = now.getNano();
//        System.out.println(nano);
//        System.out.println(UUID.randomUUID().toString().replaceAll("-","").length());
    }


}
